package com.ryan.spring.cloud.gateway.config;

import com.ryan.spring.cloud.gateway.handler.ImageCodeHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.route.InMemoryRouteDefinitionRepository;
import org.springframework.cloud.gateway.route.RouteDefinitionWriter;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.*;
import reactor.core.publisher.Mono;

/**
 * Created by kaimin on 21/1/2019.
 * time : 16:24
 */
@Configuration
public class GatewayConfig {

    @Autowired
    private ImageCodeHandler imageCodeHandler;
   /* @Bean
    public RouteLocator routeLocator(RouteLocatorBuilder builder){

        RouteLocatorBuilder.Builder routes = builder.routes();
        routes.route(r->r.path("/baidu").uri("http://www.baidu.com"))
                .route("user",r->r.path("/user/**").filters(f->f.stripPrefix(1)).uri("lb://user-center"))
                .route("common",r->r.path("/common/**").filters(f->f.stripPrefix(1)).uri("lb://common"))
                ;

        return routes.build();

    }*/



    @Bean
    public RouteDefinitionWriter routeDefinitionWriter(){


        return new InMemoryRouteDefinitionRepository();
    }

    @Bean
    public MysqlRouteDefinitionRepository mysqlRouteDefinitionRepository(){
        return new MysqlRouteDefinitionRepository();
    }


    //配置路由转发
    @Bean
    public RouterFunction routerFunction(){
        return RouterFunctions
                .route(RequestPredicates.GET("/code")
                        .and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), imageCodeHandler)
                ;
    }


}
